package com.hpin.mlxteacher.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.bean.AttendanceResult;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.List;

/**
 * Created by Aaron on 15/8/31.
 */
public class SignHistoryAdapter extends BaseAdapter {

    private Context ctx;
    private List<AttendanceResult.AttendanceDetail> datas;
    private DisplayImageOptions options;
    //上午还是中午还是下午
    private int type;

    public SignHistoryAdapter(Context ctx, List<AttendanceResult.AttendanceDetail> datas, int type) {

        this.ctx = ctx;
        this.datas = datas;
        this.type = type;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.icon_default)
                .showImageForEmptyUri(R.drawable.icon_default)
                .showImageOnFail(R.drawable.icon_default)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();

    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    public void setDatas(List<AttendanceResult.AttendanceDetail> attendanceDetails) {
        this.datas = attendanceDetails;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = View.inflate(ctx, R.layout.item_receiver, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_receiver_pic = (ImageView) convertView.findViewById(R.id.iv_receiver_pic);
            viewHolder.iv_is_select = (ImageView) convertView.findViewById(R.id.iv_is_select);
            viewHolder.tv_receiver_name = (TextView) convertView.findViewById(R.id.tv_receiver_name);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        switch (type) {

            case 0://上午

                viewHolder.iv_is_select.setSelected(datas.get(position).signM == 1);

                break;

            case 1://中午

                viewHolder.iv_is_select.setSelected(datas.get(position).signA == 1);

                break;

            case 2://下午

                viewHolder.iv_is_select.setSelected(datas.get(position).signN == 1);

                break;

        }

        //学生姓名
        viewHolder.tv_receiver_name.setText(datas.get(position).nickName);
        //头像
        ImageLoader.getInstance().displayImage(datas.get(position).picUrl, viewHolder.iv_receiver_pic, options);

        return convertView;
    }

    private class ViewHolder {
        private ImageView iv_receiver_pic;
        private ImageView iv_is_select;
        private TextView tv_receiver_name;

    }

}
